home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
317_01
/
g4sdecod.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-06-16
|
40KB
|
1,502 lines
/* $Id: g4sdecod.c 1.2 90/06/09 18:23:27 marking Exp $
*
NAME
* g4sdecod.c -- decode group 4 data using nested if statements
*
TYPE
* C procedures
*
SYNOPSIS
* char g4i_initialize (short image_width, short image_length);
* char g4i_decode (void);
*
DESCRIPTION
* In order to acquire data from the image and to return run lengths and
* new line information, these routines invoke procedures provided by the
* caller. These caller-provided procedures are invoked throught pointers
* which have been stuffed by the caller with the procedure addresses.
* To acquire a new data byte, g4i_decode () calls (*p_g4i_next_byte) ().
* To report the decoding of a black or white run, the routines
* (*p_decode_black) () or (*p_decode_white) () are called.
*
RETURNS
* Initialization always returns zero.
*
* For decoding,
* 0 end of image reached
* -1 on error (bad data)
* The decode loop will be prematurely terminated if decode_return is
* set to not zero, and the value of decode_return will be returned.
* No code here does this, but it might be useful under certain
* circumstances.
*
LEGAL
* Copyright 1989, 1990 Michael P. Marking, Post Office Box 8039,
* Scottsdale, Arizona 85252-8039. All rights reserved.
*
* License is granted by the copyright holder to distribute and use this
* code without payment of royalties or the necessity of notification as
* long as this notice (all the text under "LEGAL") is included.
*
* Reference: $Id: g4sdecod.c 1.2 90/06/09 18:23:27 marking Exp $
*
* This program is offered without any warranty of any kind. It includes
* no warranty of merchantability or fitness for any purpose. Testing and
* suitability for any use are the sole responsibility of the user.
*
HISTORY
* $Log: g4sdecod.c $
* Revision 1.2 90/06/09 18:23:27 marking
* clean up comments for release
*
* Revision 1.1 90/05/01 02:00:00 marking
* Initial revision
*
*
NOTES
*
PORTABILITY
* Tested using Microsoft C 5.1. Some memory models may not work due to
* the large decoding arrays.
*
* There is a non-portable use of "global" variables in the file g3g4.h,
* about which a minority of compilers will justifiably complain. Certain
* variables are declared in g3g4.h without extern keywords. Strictly
* speaking, they should be declared extern in all but one module, but
* that would require complication of g3g4.h. If it gets past your
* compiler and linker, you can probably ignore it.
*
SEE ALSO
* g3tdecod.c -- decode group 3 image using tables
* builddec.c -- build image decoding tables
*
INFORMATION
* Although there is no support offered with this program, the author will
* endeavor to correct errors. Updates will also be made available from
* time to time.
*
* Contact: Michael P. Marking, Post Office Box 8039, Scottsdale, Arizona
* 85252-8039 USA. Replies are not guaranteed to be swift. Beginning
* July 1990, e-mail may be sent to uunet!ipel!marking.
*
* Also beginning in July 1990, this code will be archived at the
* ipel!phoenix BBS in file g3g4.zoo. The 24-hour telephone number
* for 300/1200/2400 is (602)274-0462. When logging in, specify user
* "public", system "bbs", and password "public".
*
* This code is also available from the C Users Group in volume 317.
*/
#include "g3g4.h"
/* #define TRACE 1 */
/* implementation limits: Due to the sizes of arrays and variables, and not
due to any restrictions in the algorithm, the following limits exist:
maximum number of pixels per row: 65533
maximum number of rows per image: none
maximum or minimum k-factor: none
maximum number of runs per row: 16382 white, 16382 black
To increase (or decrease) these limits, it will be necessary to play with
array and variable sizes. On segmented machines (such as the 8086), a
different memory model may be necessary. The algorithm itself has no
limits on image size or complexity, and the stack requirements are in-
sensitive to changes in these limits or to image complexity. */
#define EVEN 0
#define ODD 1
static short a0, a1, a2, b0, b1, b2, bit_number, code_byte;
static unsigned char color, current_row, mode;
static unsigned short even_runs [32768], odd_runs [32768];
static unsigned short even_index, odd_index;
static short column_limit;
static short row_number = 0;
/* Depending as current_row == EVEN or current_row == ODD, the runs of the
current row are represented in even_runs [] or odd_runs []. The white
runs have even subscripts and the black runs have odd subscripts. The
values of the array elements are the offsets of the beginnings of the
corresponding runs from the beginning of the row. As defined by the
specification,
a0 is the reference or starting changing element on the coding line.
It may be considered the "current position".
a1 is the next changing element to the right of a0 on the coding line.
a2 is the next changing element to the right of a1 on the coding line.
b1 is the first changing element on the reference line to the right of
a0 and of opposite color to a0.
b2 is the next changing element to the right of b1 on the reference
line.
Furthermore,
b0 is the "previous" value of b1.
Depending as current_row == EVEN or == ODD, even_index or odd_index is
the subscript of the entry in even_runs [] or odd_runs [] corresponding
to the run containing the current value of a0, and its counterpart cor-
responds to the run containing b1. */
static void new_row (void);
static short decode_white_run (void);
static short decode_black_run (void);
static short decode_white_word (void);
static short decode_black_word (void);
static char next_bit (void);
static short null_mode (void);
static char decode_return;
/* g4i_decode () successively invokes (*p_decode_next_byte) () for each byte of the
encoded image, and calls (*p_decode_white) () or (*p_decode_black) () as
required to return the image contents on a run-by-run basis. */
char g4i_decode ()
{
/* At the beginning of this routine, we are in the NULL mode, which is to
to say that no codewords are currently understood or digested. The
variable bit_number has been initialized to zero, to indicate that the
next (first) codeword is to begin with bit zero of the next code byte.
This betrays an assumption that all images begin on byte boundaries, a
condition that can be changed by arranging for bit_number to be set
otherwise by g4i_initialize () or elsewhere. */
while (!decode_return)
{
mode = (unsigned char) null_mode ();
if (current_row == EVEN)
{
b0 = b1;
b1 = odd_runs [odd_index];
if ((b1 <= a0) && a0)
{
odd_index += 2;
b0 = odd_runs [odd_index - 1];
b1 = odd_runs [odd_index];
}
b2 = odd_runs [odd_index + 1];
}
else /* current_row == ODD */
{
b0 = b1;
b1 = even_runs [even_index];
if ((b1 <= a0) && a0)
{
even_index += 2;
b0 = even_runs [even_index - 1];
b1 = even_runs [even_index];
}
b2 = even_runs [even_index + 1];
}
#if defined (TRACE)
if (trace_flag)
{
if (current_row == EVEN)
{
printf ("\n ref_index=%hd b1=%hd b2=%hd a0=%hd curr[%hd]=%hd "
"color=%hd ",
odd_index, b1, b2, a0, even_index, even_runs [even_index],
(short) color);
}
else /* current_row == ODD */
{
printf ("\n ref_index=%hd b1=%hd b2=%hd a0=%hd curr[%hd]=%hd "
"color=%hd ",
even_index, b1, b2, a0, odd_index, odd_runs [odd_index],
(short) color);
}
}
#endif
switch (mode)
{
case PASS_MODE: /* skip (pass) two color changes on the previous row */
#if defined (TRACE)
if (trace_flag) printf (" P ");
#endif
if (color == WHITE) (*p_decode_white) ((short) (b2 - a0));
else /* color == BLACK */ (*p_decode_black) ((short) (b2 - a0));
a0 = b2;
if (current_row == EVEN)
{
odd_index += 2;
b1 = odd_runs [odd_index];
}
else /* curren